package org.example.electronic_fence.model;

import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
@Data
@Component
public class VirtualSensors {

    private String pwd="123456";
    Map<String,BaseModel > allVirtualSensors=new HashMap<>();

    // 统计电子围栏数量
    public int countEf() {
        int count = 0;
        for (BaseModel model : allVirtualSensors.values()) {
            if (model instanceof ElectronicFence) {
                count++;
            }
        }
        return count;
    }

    // 统计红外对射数量
    public int countHwds() {
        int count = 0;
        for (BaseModel model : allVirtualSensors.values()) {
            if (model instanceof HWDS) {
                count++;
            }
        }
        return count;
    }


    public BaseModel getVirtualSensor(String primaryKey){
        return allVirtualSensors.get(primaryKey);
    }
    public String getPrimaryKey(String d_id, String commNO,Integer devNO, Integer zoneNO ) {
        if(commNO==null)commNO="?";
        if(d_id==null)d_id="?";
        String str="";
        str+=commNO + "_" + d_id ;
        str+="_";
        if(devNO==null)str+="?";
        else str+=devNO.toString();
        str+="_";
        if(zoneNO==null)str+="?";
        else str+=zoneNO.toString();
        return str;
    }


    public String getPrimaryKey(BaseModel baseModel ){
        String commNO=baseModel.getCommNO();
        String d_id=baseModel.getD_id();
        Integer devNO=baseModel.getDevNO();
        Integer zoneNO=baseModel.getZoneNO();
        if(commNO==null)commNO="?";
        if(d_id==null)d_id="?";
        String str="";
        str+=commNO + "_" + d_id ;
        str+="_";
        if(devNO==null)str+="?";
        else str+=devNO.toString();
        str+="_";
        if(zoneNO==null)str+="?";
        else str+=zoneNO.toString();
        return str;
    }
    @PostConstruct
    public void init(){
        //添加两个电子欸蓝
        ElectronicFence electronicFence = initEf();
        electronicFence.setDevNO(1);
        electronicFence.setD_id("001");
        electronicFence.setZoneNO(1);
        ElectronicFence electronicFence2 = initEf();
        electronicFence2.setDevNO(1);
        electronicFence2.setD_id("001");
        electronicFence2.setZoneNO(2);
        allVirtualSensors.put(getPrimaryKey(electronicFence),electronicFence);
        allVirtualSensors.put(getPrimaryKey(electronicFence2),electronicFence2);

        //添加一个红外对射
        HWDS hwds = new HWDS();
        hwds.setArm(0);
        hwds.setAlarm(0);
        hwds.setDevOnline(0);
        hwds.setDevAcLoss(0);
        hwds.setDevTamper(0);
        hwds.setDevDcLow(0);
        hwds.setD_id("002");
        hwds.setDevNO(1);
        allVirtualSensors.put(getPrimaryKey(hwds),hwds);
    }

    private ElectronicFence initEf() {
        ElectronicFence electronicFence2 = new ElectronicFence();
        electronicFence2.setArm(0);
        electronicFence2.setAlarm(0);
        electronicFence2.setDevOnline(0);
        electronicFence2.setDevTamper(0);
        electronicFence2.setDevDcLow(0);
        electronicFence2.setDevAcLoss(0);
        electronicFence2.setFenceAlarm(0);
        electronicFence2.setFenceSlack(0);
        electronicFence2.setFenceCut(0);
        electronicFence2.setFenceDcVal(100);
        return electronicFence2;
    }
}
